<template>
    <PageView>
        <a-card title="列表">
            <a-button type="primary" slot="extra" @click="$router.push({name:`newEdit`})">添加新闻</a-button>
            <a-table
                :loading="loading"
                :columns="columns"
                :dataSource="data"
                :pagination="{total,pageSize,showSizeChanger,current}"
                @change="changePagination"
                bordered :scroll="{x:true}">
            </a-table>
        </a-card>
    </PageView>
</template>

<script type="text/jsx">
    import Art from '../../api/Art'
    export default {
        name: 'newsList',
        data () {
            return {
                loading: true,
                columns: [
                    {
                        title: '标题',
                        dataIndex: 'title',
                        customRender: (v) => <a>{v}</a>
                    },
                    {
                        title: '图片',
                        dataIndex: 'image',
                        customRender:(v)=>{
                            return <img style="display:block;height:40px" src={v}/>
                        }
                    },
                    {
                        title: '内容',
                        dataIndex: 'content',
                    },
                    {
                        title: '添加时间',
                        dataIndex: 'add_time',
                        customRender: (v) => this.timeOne(v)
                    },
                    {
                        title: '操作',
                        key: 'action',
                        fixed: 'right',
                        width: 110,
                        customRender: (v) => {
                            return (
                                <a-dropdown>
                                    <a-menu slot="overlay">
                                        <a-menu-item onClick={() => {
                                            this.del(v.id)
                                        }}>删除
                                        </a-menu-item>
                                        <a-menu-item onClick={() => {
                                            this.$router.push({name:"newEdit",query:{id:v.id}})
                                        }}>编辑
                                        </a-menu-item>
                                    </a-menu>
                                    <a-button>操作 <a-icon type="down"/></a-button>
                                </a-dropdown>
                            )
                        }
                    }
                ],
                data: [],
                total: 0,
                pageSize: 10,
                current: 1,
                showSizeChanger: true,
            }
        },
        activated () {
            this.getList()
        },
        methods: {
            getList () {
                this.loading = true
                Art.getNewList({
                    page: this.current,
                    num: this.pageSize,
                }).then(res => {
                    this.data = res.data.map((value, key) => {
                        return { ...value, key }
                    })
                    this.loading = false
                    this.total = parseInt(res.count)
                })
            },
            changePagination (pagination) {
                Object.keys(pagination).forEach(val => {
                    this[val] = pagination[val]
                })
                this.getList()
            },
            del(id) {
                this.$confirm({
                    title: `删除`,
                    content: `确定要删除此新闻吗?`,
                    centered: true,
                    onOk: () => {
                        return new Promise((resolve, reject) => {
                            Art.delNews({ id }).then(res => {
                                resolve()
                                this.getList()
                            }).catch(res => {
                                reject()
                            })
                        })
                    }
                })
            },
        }
    }
</script>

<style scoped lang="less">
    .inputGroup {
        > div {
            margin-bottom: 20px;
        }
    }
</style>